// BizElements.Web.CommandToolBar control.
var Biz_CommandToolBar_LockSetItemsMethod = false;

function Biz_CommandToolBar_MonitorInIntervals(sToolBarId, iItemCount, sTxtDisabledItemsId, sTxtHiddenItemsId, sTxtDisabledToolTips)
{
    var sSetItemsFunctionCall = "Biz_CommandToolBar_SetItems('" + sToolBarId + "', " + iItemCount + ", '" + sTxtDisabledItemsId + "', '" + sTxtHiddenItemsId + "', '" + sTxtDisabledToolTips + "'); ";
    setInterval(sSetItemsFunctionCall, 50);
}

function Biz_CommandToolBar_SetItems(sToolBarId, iItemCount, sTxtDisabledItemsId, sTxtHiddenItemsId, sTxtDisabledToolTips)
{
    // This may (small chance) lock SetItems method for all ToolBars on the page. But even if it does, 
    // the MonitorInIntervals method will try to invoke it again after a short period of time.
    if (Biz_CommandToolBar_LockSetItemsMethod == true)
        return;

    Biz_CommandToolBar_LockSetItemsMethod = true;
    try
    {
        var ctlDisabledItemsTxt = document.getElementById(sTxtDisabledItemsId);
        var ctlDisabledToolTipsTxt = document.getElementById(sTxtDisabledToolTips);
        var ctlHiddenItemsTxt = document.getElementById(sTxtHiddenItemsId);
        if (ctlDisabledItemsTxt == null || ctlDisabledToolTipsTxt == null || ctlHiddenItemsTxt == null)
            return;
            
        var bAlreadyProcessed = (ctlDisabledItemsTxt.value == ""  ||  ctlHiddenItemsTxt.value == "");
        if (bAlreadyProcessed)
            return;

        Biz_CommandToolBar_EnableAll(sToolBarId, iItemCount);
        
        // Disable items.
        var arrItemIDsToDisable = ctlDisabledItemsTxt.value.split(";");
        ctlDisabledItemsTxt.value = "";
        var arrToolTips = (ctlDisabledToolTipsTxt != null) ? ctlDisabledToolTipsTxt.value.split(";") : new Array();
        ctlDisabledToolTipsTxt.value = "";        
        for (var idxItem=0; idxItem<arrItemIDsToDisable.length; idxItem++)
        {
            var sDisabledItemTooltip = (arrToolTips.length > idxItem) ? arrToolTips[idxItem] : null;
            if (arrItemIDsToDisable[idxItem] != "")
                Biz_CommandToolBar_DisableItem(arrItemIDsToDisable[idxItem], sDisabledItemTooltip);
        }
        
        // Hide items.        
        var arrItemIDsToHide = ctlHiddenItemsTxt.value.split(";");        
        ctlHiddenItemsTxt.value = "";
        for (var idxItem=0; idxItem<arrItemIDsToHide.length; idxItem++)
        {
            if (arrItemIDsToHide[idxItem] != "")
                Biz_CommandToolBar_HideItem(arrItemIDsToHide[idxItem]);
        }
    }
    finally
    {
        Biz_CommandToolBar_LockSetItemsMethod = false;
    }
}

function Biz_CommandToolBar_EnableAll(sToolBarId, iItemCount)
{    
    for (var idxItem=0; idxItem<iItemCount; idxItem++)
    {
        var sItemCtlId = sToolBarId + "_n" + idxItem;
        Biz_CommandToolBar_EnableItem(sItemCtlId);        
    }
}

function Biz_CommandToolBar_EnableItem(sItemCtlId)
{    
    if (sItemCtlId == null  ||  sItemCtlId == "")
        return;
    
    // Button, TemplateButton, ImageButton...
    var ctlItem = document.getElementById(sItemCtlId);
    if (ctlItem == null)
        return;
    
    if (ctlItem.style.display == "none")
        ctlItem.style.display = "";

    if (ctlItem.BizOldTitle != null)
        ctlItem.title = ctlItem.BizOldTitle;        
        
    // Enable the button and its TD container.
    ctlItem.disabled = "";
    ctlItem.parentNode.disabled = "";
    for (var i = 0; i < ctlItem.childNodes.length; i++)
    {
        ctlItem.childNodes[i].disabled = "";
    }
    
    // Propagate events to button.
    ctlItem.parentNode.onclick = null;
}

function Biz_CommandToolBar_DisableItem(sItemCtlId, sDisabledItemTooltip)
{
    if (sItemCtlId == null  ||  sItemCtlId == "")
        return;
        
    // Button, TemplateButton, ImageButton...
    var ctlItem = document.getElementById(sItemCtlId);
    if (ctlItem == null)
        return;       
    
    if (sDisabledItemTooltip != null && sDisabledItemTooltip != "")
    {
        if (ctlItem.BizOldTitle == null)
            ctlItem.BizOldTitle = ctlItem.title;

        ctlItem.title = sDisabledItemTooltip;               
    }        
    
    // Disable the button and its TD container.
    ctlItem.disabled = "disabled";
    ctlItem.parentNode.disabled = "disabled";
    for (var i = 0; i < ctlItem.childNodes.length; i++)
    {
        ctlItem.childNodes[i].disabled = "disabled";
    }
    
    // Don't propagate events to button.
    ctlItem.parentNode.onclick = new Function("return false;");
}

function Biz_CommandToolBar_HideItem(sItemCtlId)
{
    if (sItemCtlId == null  ||  sItemCtlId == "")
        return;        

    var ctlItem = document.getElementById(sItemCtlId);
    if (ctlItem != null)
        ctlItem.style.display = "none";
}
